import "package:flutter/material.dart";
import 'package:get_it/get_it.dart';
import 'package:todo_app_open_source/controller/controller.dart';
import 'package:todo_app_open_source/pages/my_task.dart';

import '../toast.dart';

class ListTasks extends StatefulWidget {
  @override
  _ListTasksState createState() => _ListTasksState();
}

class _ListTasksState extends State<ListTasks> {
  final controller = GetIt.I.get<Controller>();
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 10.0),
      child: Wrap(
        children: controller.myTasks.asMap().keys.map((index) {
          return GestureDetector(
            child: Card(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(8.0)),
                child: Container(
                  margin: EdgeInsets.all(10.0),
                  child: Row(
                    children: <Widget>[
                      Icon(Icons.description, color: Colors.red),
                      Expanded(
                          child: Container(
                        margin: EdgeInsets.only(left: 8.0),
                        child: Text(controller.myTasks[index]),
                      )),
                      IconButton(
                        icon: Icon(Icons.create, color: Colors.blue),
                        onPressed: () =>
                            goDetail(context, controller.myTasks[index], index),
                      ),
                      IconButton(
                        icon: Icon(Icons.delete, color: Colors.blue),
                        onPressed: () =>
                            showMessage(context, controller.myTasks[index]),
                      ),
                    ],
                  ),
                )),
          );
        }).toList(),
      ),
    );
  }

  goDetail(context, value, index) {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => MyTask(editValue: value, valIndex: index)));
  }

  showMessage(context, value) {
    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text("确定要删除吗?"),
            content: Text(value),
            actions: <Widget>[
              FlatButton(
                  child: Text("取消"),
                  onPressed: () {
                    Navigator.pop(context);
                  }),
              FlatButton(
                  child: Text("确定"),
                  onPressed: () {
                    controller.deleteTask(value);
                    Navigator.pop(context);
                    Toast.toast(context, msg: "删除成功");
                  })
            ],
          );
        });
  }
}
